package com.qf.erp_system_admin.controller;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.erp_system_admin.config.ChientReadListener;
import com.qf.erp_system_admin.entity.Client;
import com.qf.erp_system_admin.service.ClientService;
import com.qf.erp_system_admin.util.R;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/tree")
public class ClientContoroller {
    private Logger logger = LogManager.getLogger(ClientContoroller.class);
    @Autowired
    private ClientService clientService;
    //查询全部
    @GetMapping("/list")
    public R findAll(@RequestParam HashMap<String,String> map) {
        int pageNum = Integer.parseInt(map.get("pageNum"));
        int pageSize = Integer.parseInt(map.get("pageSize"));
        PageHelper.startPage(pageNum,pageSize);
        List<Client> clients = clientService.findAll(map);
        PageInfo<Client> info = new PageInfo<>(clients);
        return  R.ok(info);
    }
    //单个删除
    @GetMapping("/del")
    public R del(int cliId) {
        boolean isok = clientService.del(cliId);
        return  R.ok(isok);
    }
    //批量删除
    @GetMapping ("/delBatch")
    public R delBatch(@RequestParam List<Integer> ids) {
        boolean isok =  clientService.delBatch(ids);
        return R.ok(isok);
    }
    //添加
    @PostMapping("/add")
    public R add(@RequestBody Client client) {
        System.out.println("成功接收到请求");
        boolean isok =  clientService.add(client);
        return R.ok(isok);
    }
    //更新
    @PostMapping("/up")
    public R up(@RequestBody Client client) {
        boolean isok =  clientService.up(client);
        System.out.println(client);
        return R.ok(isok);
    }
    @GetMapping("/findjs")
    public R findjs() {
        List<String> list =  clientService.findjs();
        return R.ok(list);
    }
    @GetMapping("/findywy")
    public R findywy() {
        List<String> list =  clientService.findywy();
        return R.ok(list);
    }
    @GetMapping("/find3")
    public R find3() {
        List<String> list =  clientService.find3();
        return R.ok(list);
    }
    /**
     * 上传Excel文件
     */
    @PostMapping("/excel")
    public R uploadExcel(MultipartFile file) {
        try {
            // 重点,此处第三个参数需要传入Service对象
            EasyExcel.read(file.getInputStream( ),
                            Client.class,
                            new ChientReadListener(clientService))
                    .sheet( ).doRead( );
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }
        return R.ok( );
    }
    @GetMapping("/excel1")
    public void downloadExcel(HttpServletResponse response) throws IOException {

        //  查询数据库全部数据
        List<Client> list = clientService.findAll1(null);
        System.out.println("list = " + list);

        // 导出.xls用这个
        // response.setContentType("application/vnd.ms-excel");
        // 导出.xlsx用这个
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("客户信息表", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        // easyexcel导出工具
        EasyExcel.write(response.getOutputStream(), Client.class).autoCloseStream(Boolean.FALSE).sheet("客户信息表")
                .doWrite(list);

    }

}

